/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.web.converter.entity;

import com.cgmnx.admin.common.bean.CurrentUserInfo;
import com.cgmnx.admin.common.bean.UserContext;
import com.cgmnx.admin.common.constants.WebConstant;
import com.cgmnx.admin.common.enums.ValidEnumType;
import com.cgmnx.admin.dao.entity.rbac.ResourceEntity;
import com.cgmnx.admin.model.enums.rbac.ResourceType;
import com.cgmnx.admin.web.converter.Convert;
import com.cgmnx.admin.web.model.param.ResourceParam;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: created  Date: 16/12/13 Time: 下午6:32
 * @version: 1.0
 */
@Component
public class ResourceEntityConverter implements Convert<ResourceParam, ResourceEntity> {

    @Override
    public ResourceEntity convert(ResourceParam input) {

        if (input == null) {
            return null;
        }

        ResourceEntity entity = new ResourceEntity();
        entity.setSort(input.getSort());
        entity.setName(input.getName());
        entity.setDescription(input.getDescription());
        entity.setId(input.getId());
        entity.setShowType(ResourceType.BODY);
        entity.setParentId(input.getParentId());

        entity.setUri(input.getUri());
        entity.appendTreeCode(input.getName());
        entity.setValidStatus(ValidEnumType.VALID);
        CurrentUserInfo userInfo = UserContext.getUserInfo();
        if (userInfo != null) {
            entity.setCreateUser(userInfo.getUserId());
            entity.setUpdateUser(userInfo.getUserId());
        } else {
            entity.setCreateUser(WebConstant.CURRENT_LOGIN_USER);
            entity.setUpdateUser(WebConstant.CURRENT_LOGIN_USER);
        }

        return entity;
    }

    @Override
    public List<ResourceEntity> convert(List<ResourceParam> t) {
        return null;
    }
}